/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package Modelo;

import Modelo.*;
import java.io.Serializable;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.LinkedList;
import javax.swing.*;

/**
 *
 * @author Alex
 */
public class ModelBacante implements Serializable{

	Conection con;

    public ModelBacante(Conection con) {
      this.con=con;
    }
	ResultSet rs;

	

	


 public LinkedList Bacantes(String codigo)throws SQLException{
	LinkedList listaEmpl=new LinkedList();
	PreparedStatement pstmt=null;
	ResultSet rs=null;
  	try{

  		pstmt=con.getCon().prepareStatement("select * from Bacante where Bacante.Codigo =" + codigo);
  		rs=pstmt.executeQuery();
  		while(rs.next()){
  			listaEmpl.add(Bacante.Load(rs));

  		}
  	}finally{
 		if(rs!=null){
			rs.close();
		}
		if(pstmt!=null){
			pstmt.close();
			pstmt=null;
		}
	}

	return listaEmpl;
 }
  public void setNewBacante(Bacante bacan) throws SQLException {
        if(!con.isConnect() )
                throw new SQLException("no Hay Conexion");
            PreparedStatement pstmt=null;
            try{
                pstmt=con.getCon().prepareStatement("insert into Bacante values(?,?,?,?)");
                pstmt.setString(1, bacan.getCodigo());
                pstmt.setString(2, bacan.getNombre());
                pstmt.setBoolean(3, bacan.getEstado());
                pstmt.setString(4, bacan.getEmpresa());
		
                
		pstmt.executeUpdate();

                }finally
                {
                    if( pstmt!=null )
                    pstmt.close();
                }


    }
    public void setUpdateUsuario(Bacante b )throws SQLException{
            if(!con.isConnect())
		throw new SQLException("No conexion");
            if(!Existe(b.getCodigo()))
                throw new SQLException("Existe un Usuario registrado con esta cedula, \nPorfavor seleccione Otro");
                PreparedStatement pstmt=null;
                try{
                    pstmt=con.getCon().prepareStatement("update Bacante set Codigo=?, Nombre=?, Estado=? where Codigo="+b.getCodigo());
                    pstmt.setString(1,b.getCodigo());
                    pstmt.setString(2,b.getNombre());
                    pstmt.setBoolean(3,b.getEstado());
                    
                    pstmt.executeUpdate();
                }finally
                {
                    if( pstmt!=null )
                        pstmt.close();
                }
         }
     public boolean Existe( String codigo ) throws SQLException{
            if( !con.isConnect() )
                    throw new SQLException("no conexion");
            boolean sw=true;
            PreparedStatement pstmt=null;
            ResultSet rs=null;
            try{

                            pstmt=con.getCon().prepareStatement("select count(*) from Bacante where Codigo=?");

                    pstmt.setString(1,codigo);
                    rs=pstmt.executeQuery();

                    if(rs.next()){
                        if(String.valueOf(rs.getString(1)).equals("0"))
                            sw=false;
                    }
            }finally{
                    if(rs!=null)
                            rs.close();

                    if(pstmt!=null)
                            pstmt.close();
            }
            return sw;
    }
}// fin clase model
